package au.edu.unsw.infs3634.gamifiedlearning.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.R;
import au.edu.unsw.infs3634.gamifiedlearning.model.VideoListModel;

/**
 * Created by sjt.
 * 资料列表Adapter
 * Date: 2023/2/7
 */
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> {
    private Context context;

    private List<VideoListModel> data;

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(VideoListModel model);
    }

    public VideoListAdapter(Context context, List<VideoListModel> data, OnItemClickListener listener) {
        this.context = context;
        this.data = data;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_video_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        VideoListModel videoListModel = data.get(position);
        holder.tvName.setText(videoListModel.getVideoName());
        holder.tvDate.setText(videoListModel.getVideoDate());
        holder.img.setImageResource(videoListModel.getVideoIcon());

        holder.itemView.setOnClickListener(view -> {
            if (listener != null) {
                listener.onItemClick(videoListModel);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (data == null) {
            return 0;
        }
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private ImageView img;

        private TextView tvDate;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            img = itemView.findViewById(R.id.img);
            tvDate = itemView.findViewById(R.id.tvDate);
        }
    }
}
